//
//  Symbol.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 27.03.25.
//

import Foundation
class Symbol {
    private static let add = "+"
    private static let sub = "-"
    private static let OPERATORS = add + sub
    static let opPattern = /[+-]/
    var op:String
    
    init(op: String) {
        self.op = op
    }
    
    static func calc(s1:String , s2:String , op: String) -> String {
        switch op {
            case add:
            return add(s1: s1, s2: s2)
            case sub:
            return sub(s1: s1, s2: s2)
        default:
            return "0"
        }
    }
    
    static func add(s1:String, s2:String) -> String {
        return (Double(s1)! + Double(s2)!).toStringWithoutTrailingZeros()
    }
    
    static func sub(s1:String, s2:String) -> String {
        return (Double(s1)! - Double(s2)!).toStringWithoutTrailingZeros()
    }
    
    static func isOperator(char: Character?) -> Bool {
        if (char == nil) {
            return false
        }
        return OPERATORS.contains(char!)
    }
    
    static func match(input:String) -> Bool {
        return (try? opPattern.firstMatch(in: input)) != nil
    }
}
